/*
 * main.c - orchestrate execution and connection of producer/
 *   consumer tasks.
 * by Robert Buchanan/Jacob Eichers
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "tasks.h"



int main (int argc, char** argv)
{

        int pipeA[2], pipeB[2], pipeC[2]; /* initialize pipes*/

        int pid;

        if (pipe(pipeA) || pipe(pipeB) || pipe(pipeC))
        {
                perror ("initializing pipes");
                exit(-1);
        }


        pid = fork();
        if (pid == 0)
        {
                /*
                child  process is reader 
                */
                close(pipeA[0]);
                reader(STDIN_FILENO, pipeA[1]);
                exit(0);
        } else if (pid > 0)
        {
                /*
                parent process
                */
                close (pipeA[1]);
        } else
        {
                perror("Couldn't fork");
                exit(-1);
        }


        pid = fork();
        if (pid == 0)
        {
                /*
                child  process is munch1
                */
                close(pipeB[0]);
                munch1(pipeA[0], pipeB[1]);
                exit(0);
        } else if (pid > 0)
        {
                /*
                parent process
                */
                close (pipeB[1]);
        } else
        {
                perror("Couldn't fork");
                exit(-1);
        }


        pid = fork();
        if (pid == 0)
        {
                /*
                child  process is munch2
                */
                close(pipeC[0]);
                munch2(pipeB[0], pipeC[1]);
                exit(0);
        } else if (pid > 0)
        {
                /*
                parent process
                */
                close (pipeC[1]);
        } else
        {
                perror("Couldn't fork");
                exit(-1);
        }

        writer(pipeC[0], STDOUT_FILENO);
        
       /*//WAIT FOR THINGS....*/
     


        return 0;
}

